﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Topro.Web.Controllers
{
    using Topro.Application.Interface;
    using Topro.Entity;
    using Topro.Entity.Query;
    using Topro.Entity.Response.Warehouse;

    /// <summary>
    /// 仓库控制器
    /// </summary>
    public class WarehouseController : Controller
    {
        #region 注入
        /// <summary>
        /// 日志接口
        /// </summary>
        private readonly LogService _logService;

        /// <summary>
        /// 仓库接口
        /// </summary>
        private readonly WarehouseService _warehouseService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logService">日志接口</param>
        /// <param name="warehouseService">仓库接口</param>
        public WarehouseController(LogService logService, WarehouseService warehouseService)
        {
            _logService = logService;
            _warehouseService = warehouseService;
        }
        #endregion

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public new IActionResult Index()
        {
            return View("Index");
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        public JsonResult WarehouseQuery(WarehouseQuery query)
        {
            var list = _warehouseService.GetPager(query);

            return Json(list);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <param name="id">权限编号</param>
        /// <returns></returns>
        public IActionResult AddOrEdit(int? id)
        {
            var model = new WarehouseResponse();
            if (id.HasValue)
            {
                model = _warehouseService.Get(id.Value);
            }

            return View("Edit", model);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateStatus(int id, sbyte status)
        {
            var response = new JResult()
            {
                Success = false
            };

            response = _warehouseService.UpdateStatus(id, status);

            return Json(response);
        }
    }
}
